package com.swsc.xapp.xroom;

import android.text.TextUtils;

import androidx.sqlite.db.SimpleSQLiteQuery;

import java.util.List;

public class PvDeviceInfoService extends BaseService{

    private static volatile PvDeviceInfoService instance;

    public static PvDeviceInfoService getInstance(){
        if (instance == null) {
            synchronized (PvDeviceInfoService.class) {
                if (instance == null) {
                    instance = new PvDeviceInfoService();
                }
            }
        }
        return instance;
    }
    private PvDeviceInfoService(){}

    public void saveAll(List<PvDeviceInfoDO> dos){
        db.pvDeviceInfoDao().saveCollection(dos);
    }

    public PvDeviceInfoDO getById(Long id){
        return db.pvDeviceInfoDao().getById(id);
    }

    public void deleteByTenantOrgan(Integer tenantNo, Integer organNo){
        db.pvDeviceInfoDao().deleteByTenantOrgan(tenantNo, organNo);
    }

    public List<PvDeviceInfoDO> findByCondition(Integer tenantNo, Integer organNo, String deviceType, String deviceCode, String zones){
        StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM SOLAR_DEVICE_INFO WHERE 1 = 1");
        if(null != tenantNo){
            sqlBuilder.append(" AND tenant_no = ").append(tenantNo);
        }
        if(null != organNo){
            sqlBuilder.append(" AND organ_no = ").append(organNo);
        }
        if(!TextUtils.isEmpty(deviceType)){
            sqlBuilder.append(" AND device_type = ").append(deviceType);
        }
        if(!TextUtils.isEmpty(deviceCode)){
            sqlBuilder.append(" AND device_code = ").append(deviceCode);
        }
        if(!TextUtils.isEmpty(zones)){
            String[] zoneArr = zones.split(",");
            sqlBuilder.append(" AND zone in (");
            for(int i = 0 ; i < zoneArr.length; i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append("'").append(zoneArr[i]).append("'");
            }
            sqlBuilder.append(")");
        }
        return db.pvDeviceInfoDao().doRawQuery(new SimpleSQLiteQuery(sqlBuilder.toString()));
    }

    public void save(PvDeviceInfoDO pvDeviceInfoDo) {
        db.pvDeviceInfoDao().save(pvDeviceInfoDo);
    }
}
